package com.prodavalnik.controller;

import java.io.File;
import java.util.List;

import com.prodavalnik.model.DataBaseController;
import com.prodavalnik.model.Product;

public class ResponseHTML {

	private static StringBuffer buff=new StringBuffer();
	
	public static String createHTMLFromProductList(List<Product> productList,ProductFilter p,String flag){
        		
		buff.setLength(0);
		
        for(int i = 0;i<productList.size();i++){
                Product tempProduct = productList.get(i);
                
                if(i % 3 == 0 ){
                        buff.append("<div class = \"row\">");
                }
                if(tempProduct==null)
                        return "";
                

                List<File> imageOfIthy =  tempProduct.getImages();
                if(imageOfIthy == null)
                        return "";
                
                if(flag.equals("false")){
                		appendToBuffer(tempProduct, imageOfIthy);
                }
                
                if(flag.equals("true") && DataBaseController.search(p.getName(),tempProduct)){
                	if(p==null || p.filterByName(tempProduct)){
                		appendToBuffer(tempProduct, imageOfIthy);
                	}
                }
                else if(flag.equals("true") && !DataBaseController.search(p.getName(),tempProduct)){
                	return "";
                }
                if(i%3==2){
                	 buff.append("</div>");
                }
        }
        if(productList.size()%3!=0)
        	 buff.append("</div>");
        
        return buff.toString();
}


	public static String pageWithNoSessionError(){
		StringBuffer buff = new StringBuffer();
		
		buff.append("<h1>There is no session</h1>");
		buff.append("<a href='page1.html'>Back to previous page</a>");
		return buff.toString();
	}
	
	public static String pageChooseCategory(){
		StringBuffer buff = new StringBuffer();

		buff.append("<h1>There is no category choosen</h1>");
		buff.append("<a href='page1.html'>Back to previous page</a>");
		return buff.toString();
	}
	
	private static void appendToBuffer(Product tempProduct,
			List<File> imageOfIthy) {
		buff.append("<div class = \"col-md-3\"><div class = \"thumbnail\"><img src = \"VisualizePictureWithName?name="+
		                imageOfIthy.get(0).getName()+"\" width=\"150\" height=\"150\"/> <div class = \"caption\">"+
		                "<h4>"+tempProduct.getName()+"</h4><p>"+tempProduct.getDescription()+" "+
		                tempProduct.getPrice()+"</div>"+"<button onclick=\"showProduct("+tempProduct.getId()+")\"> View </button>"+"</div></div>");
	}
	
	
}
